package com.yiwyn.demo.process.proxy;

import com.yiwyn.demo.anno.Task;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

import java.lang.reflect.Method;

@Slf4j
@Component
public class TaskProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

        Class<?> targetClass = AopUtils.getTargetClass(bean);

        Method[] declaredMethods = targetClass.getDeclaredMethods();

        for (Method declaredMethod : declaredMethods) {
            declaredMethod.setAccessible(true);
            Task annotation = AnnotationUtils.findAnnotation(declaredMethod, Task.class);
//            if (declaredMethod.isAnnotationPresent(Task.class)) {
//                log.info("获取到Task注解的类：{}", declaredMethod.getName());
//            }
            if (annotation != null) {
                log.info("获取到Task注解的类：{} , 注解信息:{}", declaredMethod.getName(), annotation);
            }
        }
        return bean;
    }
}
